<?php
if (!isset($_SESSION)) {
	session_start();
	if(!isset($_SESSION['username'])){
		$_SESSION['username']='';
	}
}
if($_SESSION['username'] == NULL) {
	exit("<p>游客请<a href='login.php?action=ask'>登录</a>!</p>");
}

if (!empty($_POST))
{	$username=$_SESSION['username'];
	$title=trim($_POST['title']);
	$content=trim($_POST['contents']);
	$posttime=date("Y-m-d H:i:s",time());

	require_once('lib/connbae.php');
	
	if(strlen($title)>128||
		strlen($content)>9999){
		exit('检查输入,<a href="javascript:history.back(-1);">返回</a>');
	}
	


	$qid=mysql_result(mysql_query('select max(qid) FROM Question'),0);
	$qid+=1;
	
	$sql="INSERT INTO Question(qid,title,username,content,category,post_time,total_view,total_reply)VALUES('$qid','$title','$username','$content','$tag','$posttime','0','0')";
	
	$result =mysql_query($sql);
	
	if (!$result) {
		mysql_close();
		echo "\n".mysql_error()."\n".$sql."\n";
		exit('操作失败,<a href="javascript:history.back(-1);">返回</a>');
	}
	
	if(!empty($_REQUEST ["tag"]))
	{
		if(count($_REQUEST ["tag"])>5)
			exit('标签最多5个, <a href="javascript:history.back(-1);">返回</a>');
			
		$tag="";
		
		foreach($_POST['tag'] as $i)
		{
			$tag .= $i; 
			mysql_query("UPDATE Category_count SET tag_count=tag_count+1 WHERE tag_id='$i';");
		}
	}
	
	mysql_close();
	
    header("Location:/");
}
?>
<!DOCTYPE HTML>
<html>
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>C Programing Learning</title>
	<link rel="stylesheet" type="text/css" href="css/base.css"/>
	<link rel="stylesheet" type="text/css" href="css/ask.css"/>
	<link href="img/favour.ico" type="image/ico" rel="shortcut icon">
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript"><!--
	$(document).ready(function(){
		$("label.tag").click(function(){
			if(CheckCount()>4){color='red';}else{color='green';}		
			this.style.background=((document.getElementById(
				this.getAttributeNode("for").value).checked?false:true)? color:'');
		});
		$("input.submit").click(function(){
			var j=CheckCount()
			//warning.innerText=CheckCount();
			if(j>5){
				document.getElementById("warning").innerHTML="<p style='color:red'>←最多选择<b>5</b>个</p>";
				return false;
			}else if(j==0){
				document.getElementById("warning").innerHTML="<p style='color:red'>←至少选择<b>1</b>个分类</p>";
				return false;
			}else{
			document.getElementById("warning").innerText="";}
					
		});
		function CheckCount(){
			var j = 0;
			var ckb = $("input.checkbox");
			for(var i=0;i<ckb.length;i++){
				if(ckb[i].checked==true){j++;}
			}
			return j;
		}
	});

	function checktext(text) 
	{ 
		allValid = true; 
		for (i = 0; i < text.length; i++) 
		{ 
			if (text.charAt(i) != " ")
			{ 
				allValid = false; 
				break; 
			} 
		} 
		return allValid; 
	}

	function checkinput(message,total,used,remain) 
	{
		var max; 
		max = total.value; 
		if (message.value.length > max) { 
			message.value = message.value.substring(0,max); 
			used.value = max; 
			remain.value = 0; 
		} 
		else { 
			used.value = message.value.length; 
			remain.value = max - used.value; 
		} 
	}

	-->
	</script>
</head>
<body>
	<div id="content">
		<div id="container">
			<div id="header">
				<div class="nav">
					<ul>
						<li><a class="logo" href="/" title="Funny C"></a></li>
						<li><a href="/">主页</a></li>
						<li><a href="ask.php">提问</a></li>
						<li><a href="documation.php">资源</a></li>
<?php if(!isset($_SESSION['username'])||$_SESSION['username']=='')
					{
echo "						<li><a href='login.php?action=login'>登录</a></li>
						<li><a href='register.php'>注册</a></li>";
					}else{
echo "						<li><a href='my.php'>个人中心</a></li>
						<li><a href='login.php?action=logout'>注销</a></li>\n";
					} ?>
					</ul>
				</div>	
			</div><hr/>
			<div id="ask">
				<form action="ask.php" method="post">
					<p>
						<label for="title" class="describle">一句话描述您的疑问:</label><br/>
						<textarea id="title" name="title" rows="2" cols="32" wrap=PHYSICAL
								placeholder="请在这里概述您的问题"
								oninput="checkinput(this.form.title,this.form.total,this.form.used,this.form.remain);"
								onpropertychange=="checkinput(this.form.title,this.form.total,this.form.used,this.form.remain);"
									></textarea>
						<span class="count">
							<input disabled maxLength="2" name="used" size="2" value="0" class="inputused"/>/<input disabled maxLength="2" name="total" size="2" value="32" class="inputtotal"/>				 
							<input disabled maxLength="4" name="remain" size="3" value="32" class="inputremain"  
								style="display:none;"/>
						</span>
					</p> 				
					<p>
						<label for="contents" class="describle">问题补充(选填)</label><br/>
						<textarea id="contents" name="contents" rows="9" cols="32" wrap=PHYSICAL
								placeholder="在这里继续补充问题细节"
								oninput="checkinput(this.form.contents,this.form.total2,this.form.used2,this.form.remain2);" 
								onpropertychange=="checkinput(this.form.contents,this.form.total2,this.form.used2,this.form.remain2);"
									></textarea>
					</p>
					<p>
						<span class="count">
							<input disabled maxLength="4" name="used2" size="3" value="0" 
								class="inputused" style="display:none;"/>
							<input disabled maxLength="4" name="total2" size="3" value="9999" 
								class="inputtotal" style="display:none;"/>				 
							您还可以输入<input disabled maxLength="4" name="remain2" size="3" value="9999" class="inputremain"/>字
						</span>
						<span>
<ul>
<li><label class='tag0'><b>问题分类-</b></label></li>
<?php
require_once('lib/connbae.php');
$tags =mysql_query("SELECT tag_id,tag_name FROM Category");
while($rows = mysql_fetch_array($tags)){
echo "<li><label for='tag{$rows['tag_id']}' class='tag'>{$rows['tag_name']}</label><input id='tag{$rows['tag_id']}' type='checkbox' name='tag[]' value='{$rows['tag_id']}' class='checkbox'/></li>\n";}
mysql_close();
?>
<li><label id="warning"></label></li>
</ul>

						</span>
						<hr class="clear"/>
						<span><input type="submit" value="提问" class="submit"/></span>
						
					</p>
				</form>
			</div>
		</div>
		<div id="footer">
			<p>©2014 C语言在线答疑系统 v 1.0</p>	
		</div>	
	</div>
</body>
</html>